package com.myzl.coal.client.channel.init;

import com.myzl.coal.client.channel.init.base.TcpChannelInitializer;
import com.myzl.coal.client.parse.WebSocketFrameHandler;
import io.netty.channel.ChannelPipeline;
import io.netty.channel.socket.SocketChannel;
import io.netty.handler.codec.http.HttpObjectAggregator;
import io.netty.handler.codec.http.HttpServerCodec;
import io.netty.handler.codec.http.websocketx.WebSocketServerProtocolHandler;
import io.netty.handler.stream.ChunkedWriteHandler;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;


/**
 * @ClassName ClientChannelInitializer
 * @Author wzb
 * @Date 2023/1/18 16:08
 * @Description
 */
@Data
@AllArgsConstructor
@Component
@Slf4j
public class WebSocketChannelInitializer extends TcpChannelInitializer {
    @Override
    protected void initChannel(SocketChannel ch) throws Exception {
        ChannelPipeline pipeline = ch.pipeline();
        pipeline.addLast(new HttpServerCodec());
        //以块方式写
        pipeline.addLast(new ChunkedWriteHandler());
        //http数据在传输过程分段,HttpObjectAggregator 可以分段聚合
        pipeline.addLast(new HttpObjectAggregator(8192));
        pipeline.addLast(new WebSocketServerProtocolHandler("/ttt"));
        pipeline.addLast(new WebSocketFrameHandler());
    }


}
